import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
const path = require('path')
// svg插件(vite-plugin-svg-icons) https://github.com/anncwb/vite-plugin-svg-icons/blob/main/README.zh_CN.md
import viteSvgIcons from 'vite-plugin-svg-icons'
/**
 * https://vitejs.dev/config/
 * @type {import('vite').UserConfig}
 */
// https://vitejs.dev/config/
export default defineConfig({
  base: './',
  server: {
    open: true,
    port: 9527,
    proxy: {
      // 简易匹配
      // '/foo': 'http://localhost:4567/foo',
      // 字符串匹配
      '/api': {
        target: 'https://www.fastmock.site/mock/e1c084430e876d43253d3a66b4170d25/egg-admin/',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      },
      // 正则匹配
      '^/fallback/.*': {
        target: 'http://jsonplaceholder.typicode.com',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/fallback/, '')
      }
    }
  },
  plugins: [
    vue(),
    viteSvgIcons({
      // 指定需要缓存的图标文件夹
      iconDirs: [path.resolve(process.cwd(), 'src/icons')],
      // 指定symbolId格式
      symbolId: 'icon-[dir]-[name]'
    })
  ],
  // 别名
  resolve: {
    // extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'], // 导入时想要省略的扩展名列表 .vue不支持
    // 别名
    alias: {
      '@': path.resolve(process.cwd(), 'src'),
    },
  },
  css: {
    postcss: {
      // 避免打包时 控制台打印 warning: "@charset" must be the first rule in the file
      plugins: [
        {
          postcssPlugin: 'internal:charset-removal',
          AtRule: {
            charset: (atRule) => {
              if (atRule.name === 'charset') {
                atRule.remove()
              }
            }
          }
        }
      ],
    },
  },
  build: {
    // cssCodeSplit: false, // 启用/禁用 CSS 代码拆分 禁用后 css拆成独立文件
    sourcemap: true, // 是否打包sourcemap
    rollupOptions: { // 多应用页面配置
      /* input: {
         main: path.resolve(__dirname, 'index.html'),
         nested: path.resolve(__dirname, 'nested/index.html')
       }*/
    }
  }
})
